var app = getApp()
const date = new Date()
const years = []
const months = []
const days = []
const hours = []

for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}

for (let i = 1; i <= 12; i++) {
  months.push(i)
}

for (let i = 1; i <= 31; i++) {
  days.push(i)
}
for (let i = 0; i <= 23; i++) {
  hours.push(i)
}
Page({
  data: {
    years,
    months,
    days,
    hours,
    year: date.getFullYear(),
    month: 1,
    day: 2,
    hour: 0,
    value: [9999, 1, 1,1],
    result1: '',
    result2: '',
    val: "",
    show: false,
    birth: '',
    xingzuo: ''
  },
  onLoad: function () {
    this.getKey()
  },
  getKey() {
    let that = this
    wx.cloud.callFunction({
      name: 'getKey',
      data: {
        name: 'birth'
      }
    }).then(res => {
      console.log(res.result.data[0].key)
      that.setData({
        birth: res.result.data[0].key
      })
    })
    wx.cloud.callFunction({
      name: 'getKey',
      data: {
        name: 'xingzuo'
      }
    }).then(res => {
      console.log(res.result.data[0].key)
      that.setData({
        xingzuo: res.result.data[0].key
      })
    })
  },
  

  /**
   * 获取用户输入 
   */
  bindChange: function (e) {
    const val = e.detail.value
    this.setData({
      year: this.data.years[val[0]],
      month: this.data.months[val[1]],
      day: this.data.days[val[2]],
      hour: this.data.hours[val[3]],
    })
  },
  // 事件处理函数
  onInput: function (e) {
    var that = this;
      wx.request({
        url: "https://apis.juhe.cn/birthEight/query?key="+that.data.birth+"&year="+that.data.year+"&month=" + that.data.month + "&day=" + that.data.day + "&hour=" + that.data.hour,
        data: {
        },
        header: {
          'content-type': 'application/json' // 默认值
        },
        success: function (res) {
          console.log(res)
          wx.hideLoading();
          if (res.statusCode == 200) {
            that.setData({
              result1: res.data.result,
              show: true
            })
            wx.request({
              url: "https://apis.juhe.cn/fapig/constellation/query?key="+that.data.xingzuo+"&keyword="+that.data.result1.astro,
              data: {
              },
              header: {
                'content-type': 'application/json' // 默认值
              },
              success: function (res) {
                console.log(res)
                wx.hideLoading();
                if (res.statusCode == 200) {
                  that.setData({
                    result2: res.data.result,
                    show: true
                  })
                  
                } else {
                  that.setData({
                    result: '服务器错误！',
                    show: false
                  })
                }
              },
              fail(res){
                console.log(res)
              }
            })
          } else {
            that.setData({
              result: '服务器错误！',
              show: false
            })
          }
        },
        fail(res){
          console.log(res)
        }
      })

  },

})
